package io.github.jsp.dao;

import io.github.jsp.model.JobLog;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface JobLogMapper {
    
    @Select("SELECT * FROM sys_job_log WHERE id = #{id}")
    JobLog selectById(Long id);
    
    @Select("SELECT * FROM sys_job_log WHERE job_id = #{jobId} ORDER BY id DESC")
    List<JobLog> selectByJobId(Integer jobId);
    
    @Select("SELECT * FROM sys_job_log ORDER BY id DESC LIMIT #{limit} OFFSET #{offset}")
    List<JobLog> selectWithPage(@Param("offset") int offset, @Param("limit") int limit);
    
    @Select("SELECT COUNT(*) FROM sys_job_log")
    int countAll();
    
    @Select("SELECT * FROM sys_job_log WHERE job_id = #{jobId} ORDER BY id DESC LIMIT #{limit} OFFSET #{offset}")
    List<JobLog> selectByJobIdWithPage(@Param("jobId") Integer jobId, @Param("offset") int offset, @Param("limit") int limit);
    
    @Select("SELECT COUNT(*) FROM sys_job_log WHERE job_id = #{jobId}")
    int countByJobId(Integer jobId);
    
    @Insert("INSERT INTO sys_job_log (job_group, job_id, executor_address, executor_handler, executor_param, " +
            "executor_sharding_param, executor_fail_retry_count, trigger_time, trigger_code, trigger_msg, " +
            "handle_time, handle_code, handle_msg, alarm_status) " +
            "VALUES (#{jobGroup}, #{jobId}, #{executorAddress}, #{executorHandler}, #{executorParam}, " +
            "#{executorShardingParam}, #{executorFailRetryCount}, #{triggerTime}, #{triggerCode}, #{triggerMsg}, " +
            "#{handleTime}, #{handleCode}, #{handleMsg}, #{alarmStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(JobLog jobLog);
    
    @Update("UPDATE sys_job_log SET handle_time=#{handleTime}, handle_code=#{handleCode}, " +
            "handle_msg=#{handleMsg} WHERE id=#{id}")
    int updateHandleInfo(JobLog jobLog);
    
    @Delete("DELETE FROM sys_job_log WHERE id = #{id}")
    int deleteById(Long id);
    
    @Delete("DELETE FROM sys_job_log WHERE job_id = #{jobId}")
    int deleteByJobId(Integer jobId);
    
    @Delete("DELETE FROM sys_job_log WHERE trigger_time < #{beforeTime}")
    int deleteBeforeTime(@Param("beforeTime") java.sql.Timestamp beforeTime);
}